﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyMvc.Model;
using MyMvc.BLL;
using MyMvc.Model.Models;
using MyMvc.IBLL;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Security;

namespace GreenProtect.service
{
    public partial class topic : System.Web.UI.Page
    {
        public int idid;
        IArticleService _articleService = new ArticleService();
        IAboutArticleService _aboutService = new AboutArticleService();
        protected void Page_Load(object sender, EventArgs e)
        {
            int ids;
            List<int> list=new List<int>();
            string aaaaa = Request.QueryString["ID"];
            string bbbbb = Server.HtmlDecode(Request["ID"]);
            int id = Convert.ToInt32(Request.QueryString["ID"]);
            if (id==null&&Server.HtmlDecode(Request["ID"]) == null)
            {
                ids = 1;//默认公司简介页面
            }
            else if (id == null && Server.HtmlDecode(Request["ID"]) != null)
            {
                string idss = Server.HtmlDecode(Request["ID"]);

                ids = Convert.ToInt32(Server.HtmlDecode(Request["ID"]));//去到当前文章的ID,并查处该文章信息
            }
            else {
                ids = id;
            }
            var currentArticle = _articleService.LoadEntities(c => c.ID == ids).FirstOrDefault();

            var currentAbout = _aboutService.LoadEntities(c => c.ArticleID == ids).ToArray();

            foreach (var a in currentAbout)
            {
                var ab = a.AboutID;
                list.Add(ab);
            }

            //展示相关文章的信息
            for (int i = 1; i < list.Count; i++)
            {
                int j = currentAbout[i].AboutID;
                if (i == 1 && j != null)
                {
                    var aboutArticle = _articleService.LoadEntities(c => c.ID == j).FirstOrDefault(); ;
                    string aboutTitle = aboutArticle.Title;
                    DateTime aboutTime = aboutArticle.CreateDateTime;
                    idid = j;
                    about01.InnerHtml = aboutTitle;
                    about11.InnerHtml = aboutTime.ToString();
                    about01.Attributes.Add("href","topic.aspx?id="+j);
                }
                else if (i == 2 && j != null)
                {
                    var aboutArticle = _articleService.LoadEntities(c => c.ID == j).FirstOrDefault(); ;
                    string aboutTitle = aboutArticle.Title;
                    DateTime aboutTime = aboutArticle.CreateDateTime;
                    about02.InnerHtml = aboutTitle;
                    about12.InnerHtml = aboutTime.ToString();
                    about02.Attributes.Add("href", "topic.aspx?id=" + j);
                }
                else if (i == 3 && j != null)
                {
                    var aboutArticle = _articleService.LoadEntities(c => c.ID == j).FirstOrDefault(); ;
                    string aboutTitle = aboutArticle.Title;
                    DateTime aboutTime = aboutArticle.CreateDateTime;
                    about03.InnerHtml = aboutTitle;
                    about13.InnerHtml = aboutTime.ToString();
                    about03.Attributes.Add("href", "topic.aspx?id=" + j);
                }
                else if (i == 4 && j != null)
                {
                    var aboutArticle = _articleService.LoadEntities(c => c.ID == j).FirstOrDefault(); ;
                    string aboutTitle = aboutArticle.Title;
                    DateTime aboutTime = aboutArticle.CreateDateTime;
                    about04.InnerHtml = aboutTitle;
                    about14.InnerHtml = aboutTime.ToString();
                    about04.Attributes.Add("href", "topic.aspx?id=" + j);
                }
                else
                {
                    about00.InnerHtml = "无相关文章。";
                }
            }

            //展示文章
            string title1 = currentArticle.Title;
            string author1 = currentArticle.Author;
            DateTime creattime1 = currentArticle.CreateDateTime;
            string content1 = currentArticle.Content1;
            int count1 = (int)currentArticle.Count;
            title.InnerHtml = title1;
            //category.InnerHtml = currentArticle.Category;
            count.InnerHtml = count1.ToString();
            content.InnerHtml = content1;
            creattime.InnerHtml = creattime1.ToString();
            author.InnerHtml = author1;
            

        }
    }
}